<template>
  <div ref="main" :style="{height: height, width: width}" :class="className">
  </div>
</template>

<script>
import * as echarts from 'echarts';
export default{
    props:{
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '400px'
        },
        xAxisData: {
            type: Array,
            default: () => ['2022-02', '2022-03', '2022-04', '2022-05','2022-06', '2022-07']
        },
        seriesData: {
            type: Array,
            default: () => [10, 50, 200, 350, 230, 70]
        }
    },

    data(){
        return{
            chart: null
        }
    },

    // 组件销毁之前调用这个钩子函数
    beforeDestroy(){
        if(!this.chart){
            return;
        }
        this.chart.dispose();
        this.chart = null;
    },

    mounted(){
        this.$nextTick(() => {
            this.initChart();
        })
    },

    methods: {
        initChart(){
            this.chart = echarts.init(this.$refs.main);
            this.chart.setOption({
                color: ['#3398DB'],
                title: {
                    text: '近6个月发布的文章数',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                legend:{
                    orient: 'vertical',
                    left: 'left',
                    data: this.legendData
                },
                grid:{
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                        data: this.xAxisData,
                        axisTick: {
                            alignWithLabel: true
                        }
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        name: '发布数',
                        type: 'bar',
                        barWidth: '60%',
                        data: this.seriesData
                    }
                ]
            })
        }
    }
}

</script>
